﻿using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using System.Web;

namespace YouHP.Pages
{
    public class Prescription : BasePage
    {
        protected int _pid = 0;
        protected PrescriptionInfo _prescriptionInfo;
        protected DoctorInfo _doctorInfo;

        protected string _description;

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected void HandleCommon()
        {
            // Get the prescription and doctor data.
            int _pid = RequestTools.GetQueryInt("pid", 0);
            if (_pid <= 0)
            {
                this.AddErrLine("找不到该中药方!");
                return;
            }

            _prescriptionInfo = PrescriptionBLL.Instance.GetPrescriptionInfo(_pid);
            if (_prescriptionInfo.Id <= 0)
            {
                this.AddErrLine("找不到该中药方!");
                return;
            }

            _doctorInfo = DoctorBLL.Instance.GetDoctorInfo(_prescriptionInfo.DoctorId);
            if (_doctorInfo.Id <= 0)
            {
                this.AddErrLine("找不到药方对应的医生信息");
                return;
            }

            // Set the locations.
            this.AddLocation("中药列表", "/PrescriptionAll.aspx");
            this.AddLocation(string.Format("{0}", DiseaseBLL.Instance.GetDiseaseInfo(_doctorInfo.DiseaseId).Name),
                            string.Format("/DoctorList.aspx?diseaseid={0}", _doctorInfo.DiseaseId));
            this.AddLocation(string.Format("{0}", _doctorInfo.Name), 
                string.Format("/Doctor.aspx?doctorid={0}", _doctorInfo.Id));
            this.AddLocation(_prescriptionInfo.Name, "");

            this.SetTitle(string.Format("中药 - {0}", _prescriptionInfo.Name));
        }
        
        protected void HandlePost()
        {

        }

        protected void HandleGet()
        {


            String desc = _prescriptionInfo.Description;
            if (desc != "")
            {
                string[] lines = desc.Split(new char[] { '\n' });
                for (int lineIdx = 0; lineIdx < lines.Length; ++lineIdx)
                {
                    _description += lines[lineIdx];
                    _description += "<br>";
                }
            }
            
        }
    }
}
